package com.willpower.eureka.controller;

import com.willpower.eureka.auth.response.Result;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.nio.charset.StandardCharsets;

/**
 * @author: wen-yi;
 * @date: 2021/10/31 21:27;
 * @Description: 资源
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/getCurrentUser") //oauth2解析 直接放入 Bearer Token ： 获取的信息 并不完整
    public Object getCurrentUser(Authentication authentication){
        return authentication;
    }

    @RequestMapping("/getAllCurrentUser")
    public Object getAllCurrentUser(Authentication authentication, HttpServletRequest request){
        String header = request.getHeader("Authorization");
        String token = header.substring(header.indexOf("Bearer ") + 7);
        return Jwts.parser()
                // 主要密钥编码
                .setSigningKey("wen-yi".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
    }
}
